<template>
  <div>
    <el-divider content-position="right">
      <i
        class="fontFamily-24"
        @click="handerdz"
        :class="isgood === true ? 'dz cms-dianzan1' : 'cms-dianzan2'"
        ><span style="font-size: 18px">{{ goodNum }}</span></i
      >&nbsp;&nbsp;

      <i class="cms-huifupingluns fontFamily-24 pl" @click="$emit('focusev')"
        >&nbsp;{{ commentcount }}</i
      >
    </el-divider>
  </div>
</template>

<script>
import { goodLike } from '../../api/detail'
import { isAuth, getTokenValueData } from '../../utils/auth'
import bus from '../../lib/globalBus'
export default {
  data () {
    return {
      isgood: false,
      iscomment: false,
      goodNum: this.like
    }
  },
  props: {
    commentcount: {
      type: Number,
      default: 0
    },
    like: {
      type: Number,
      default: 0
    }
  },
  mounted () {
    bus.$on('likeEvt', (e) => {
      this.isgood = e.liked
      this.goodNum = e.like
    })
  },
  methods: {
    //   点赞
    async handerdz () {
      if (!isAuth()) {
        this.$router.push('/login')
        return
      }

      this.isgood = !this.isgood
      console.log(this.$route.params.id, {
        usersId: parseInt(getTokenValueData().id),
        liked: this.isgood
      })

      const res = await goodLike(this.$route.params.id, {
        usersId: parseInt(getTokenValueData().id),
        liked: this.isgood
      })
      console.log('点赞ok', res)

      if (this.isgood === true) {
        this.goodNum++
      } else {
        this.goodNum--
      }
    }
  }
}
</script>

<style lang='scss' scope>
i {
  cursor: pointer;
}
.dz span {
  color: #ff9d00;
}
</style>
